package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOP.service.EmpNotificationMessageService;
import com.burgeon.backend.SHOPBACKEND.entity.ErpUserAndEmp;
import com.burgeon.backend.SHOPBACKEND.entity.FOrder;
import com.burgeon.backend.SHOPBACKEND.entity.ShopKf;
import com.burgeon.backend.SHOPBACKEND.entity.YcVRetail;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendOrderMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.WeChatKfMapper;
import com.burgeon.backend.SHOPBACKEND.push.UmengUtil;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryEmployee;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryYcRetailByDoNo;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryYcRetalloutByIsTs;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryYcVRetail;
import com.burgeon.backend.SHOPBACKEND.service.JpushService;
import com.burgeon.backend.SHOPBACKEND.service.TempletOrderService;
import com.burgeon.backend.Util.JiPushUtil;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service("myService")
public class TempletOrderServiceImpl implements TempletOrderService {
    private final static Log logger = LogFactory.getLog(TempletOrderService.class);
    @Autowired
    JiPushUtil jiPushUtil;
    @Autowired
    private BackendOrderMapper backendOrderMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private EmpNotificationMessageService empNotificationMessageService;
    @Autowired
    private WeChatKfMapper weChatKfMapper;
    /**
     * 获取超时未发货订单，发送给导购
     */
    public void queryOverTime(){
        //a.获取现在的时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //String nowDate = sdf.format(date);
        //b.友盟工具
        UmengUtil umengUtil = new UmengUtil();
        //1.获取所有客户
        List<Info> accList = accountMapper.getShopAccountAll();
//        List<Acc> accList = backendOrderMapper.QuerryAccIsApp();
        for (Info info: accList) {
            //String account = acc.getAccount();
            //2.根据账号查询客户的账号信息
            //nfo info = GetInfo(account);
            //3.根据未提交，已推送，查询订单信息
            QuerryYcRetalloutByIsTs querryYCRetailout = new QuerryYcRetalloutByIsTs(
                    info.getUrl(),info.getAccount(),info.getPassword());
            //根据店仓查询员工信息
            QuerryEmployee querryEmployee = new QuerryEmployee(
                    info.getUrl(), info.getAccount(), info.getPassword());
            try {
                ArrayList<FOrder> fOrders = querryYCRetailout.getData();
                ArrayList<FOrder> overTimeFOrders = new ArrayList<>();
                //3.1遍历订单信息
                for (FOrder fOrder:fOrders) {
                    //3.2比较时间是否超时
                    //3.3统一时间格式
                    Date creationDate = sdf.parse(fOrder.getCreationDate());
                    //3.4相差时长超过一小时
                    long cha = date.getTime() - creationDate.getTime();
                    double result = cha * 1.0 / (1000 * 60 * 60);
                    if (result > 48) {
                        overTimeFOrders.add(fOrder);
                        //3.4.1通过友盟推送给客服
                        //根据发货店仓查询员工
                        // ArrayList<Employee> employees = querryEmployee.getData(fOrder.getcStoreId());//fOrder.getcStoreId()"石家庄长安万达店"

                        //判断是否有指挥导购
//                        if("Y".equals(info.getIs_app())) {
//                            //极光推送的参数号列
//                            List<String> aliasList = new ArrayList<>();
//                            for (Employee employee : employees) {
//                                if (!"null".equals(employee.getHandset())) {
//                                    aliasList.add(employee.getHandset());
//                                }
//                            }
//                            JiPushMsg jiPushMsg = new JiPushMsg();
//                            //jiPushMsg.setAliasList(aliasList);
//                            jiPushMsg.setNotification_title("店铺未及时发货通知");
//                            jiPushMsg.setMsg_title("店铺未及时发货通知");
//                            jiPushMsg.setMsg_content(fOrder.getcStoreId() + "店铺" + fOrder.getDocno() + "订单，超时发货 需要人工干预");
//                            jiPushMsg.setExtrasparam(fOrder.getId());
//                           // jiPushMsg.setAccount(account);
//
//                            String channel = "com.bojun.rfmapplication.activity.home.OrderListActivity";
//                            if (aliasList.size() != 0) {
//                                String aliass = "";
//                                for (String alia : aliasList) {
//                                    aliass += alia + ",";
//                                }
//                                String a = aliass.substring(0, aliass.lastIndexOf(","));
////                              System.err.println("截取之后的"+a);
//                                //通过友盟发送给导购
//                                Boolean t = umengUtil.sendAndroidCustomizedcast(a, "phone",
//                                        jiPushMsg.getNotification_title(), jiPushMsg.getMsg_title(),
//                                        jiPushMsg.getMsg_content(), jiPushMsg.getExtrasparam(), channel, "3");
//                                //System.err.println("发送返回的状态是："+t);
//                            }
//                        }else {//没有智慧导购的

//                    }

                    }




                }
                System.err.println(overTimeFOrders);
                if(overTimeFOrders.size() != 0){
                    ArrayList<ShopKf> shopKfs = weChatKfMapper.getKf(info.getToken());
                    for (ShopKf shopKf : shopKfs) {
                        //QuerryYcRetailByDoNo querryYcRetailByDoNo = new QuerryYcRetailByDoNo(info.getUrl(), info.getAccount(), info.getPassword());
//                            ErpUserAndEmp erpUserAndEmp = querryYcRetailByDoNo.getData(fOrder.getYcDocno());
//                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        // String payDate = simpleDateFormat.format(erpUserAndEmp.getCreatTime());
                        empNotificationMessageService.sendEmpOvertime(shopKf.getOpenid(),shopKf.getToken());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 手工派单通知
     */
    @Override
    public void pushPaiDan() {
        //获取所有客户
        List<Info> accList = accountMapper.getShopAccountAll();
        //b.友盟工具
        UmengUtil umengUtil = new UmengUtil();

        for (Info info:accList) {
            QuerryYcVRetail querryYCRetailout = new QuerryYcVRetail(info.getUrl(),info.getAccount(),info.getPassword());
            QuerryYcRetailByDoNo querryYcRetailByDoNo = new QuerryYcRetailByDoNo(info.getUrl(),info.getAccount(),info.getPassword());
            try {
                //查询失败的云仓派单
                ArrayList<YcVRetail> ycVRetails = querryYCRetailout.getData();
                if(ycVRetails.size()!=0) {
                    ArrayList<ShopKf> shopKfs = weChatKfMapper.getKf(info.getToken());

                    for (ShopKf shopKf : shopKfs) {
                        empNotificationMessageService.sendEmpDelivery(shopKf.getOpenid(),shopKf.getToken());
                    }
                }


//                if("Y".equals(info.getIs_app())){
//                    for (YcVRetail ycVRetail:ycVRetails) {
//                        //根据单号查询云仓订单购买顾客，导购手机号
//                        ErpUserAndEmp erpUserAndEmp = querryYcRetailByDoNo.getData(ycVRetail.getDoNo());
//                        if(!"null".equals(ycVRetail.getDoNo())){
//                            if(!"null".equals(erpUserAndEmp.getUserEmpPhone())){
//                                System.err.println(erpUserAndEmp.getUserEmpPhone()+ycVRetail.getDoNo());
//
//                                if("Y".equals(info.getIs_app())){//判断有智慧导购app
//                                    JiPushMsg jiPushMsg =new JiPushMsg();
//                                    //jiPushMsg.setAliasList(aliasList);
//                                    jiPushMsg.setNotification_title("手工分单通知");
//                                    jiPushMsg.setMsg_title("手工分单通知");
//                                    jiPushMsg.setMsg_content(ycVRetail.getDoNo()+"订单，派单失败 需要人工干预");
//                                    jiPushMsg.setExtrasparam(ycVRetail.getId());
//                                    //jiPushMsg.setAccount(account);
//
//                                    String channel = "com.bojun.rfmapplication.activity.home.OrderListActivity";
//                                    //通过友盟发送给导购
//                                    Boolean t = umengUtil.sendAndroidCustomizedcast(erpUserAndEmp.getUserEmpPhone(), "phone",
//                                            jiPushMsg.getNotification_title(), jiPushMsg.getMsg_title(),
//                                            jiPushMsg.getMsg_content(), jiPushMsg.getExtrasparam(), channel, "3");
//                                    //System.err.println("发送返回的状态是："+t);
//                                }else {
//                                    ArrayList<ShopKf> shopKfs = weChatKfMapper.getKf(info.getToken());
//                                    for (ShopKf shopKf : shopKfs) {
//                                        empNotificationMessageService.sendEmpDelivery(
//                                                shopKf.getKfNick(),erpUserAndEmp.getUserName(),erpUserAndEmp.getUserPhone(),shopKf.getOpenid(),ycVRetail.getId(),info.getToken());
//                                    }
//
//                                }
//
//
//                            }
//                        }
//
//
//
//                    }
//
//                }else {//没有智慧导购的


//                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }
}
